package com.xiaoke.api.controller;

import com.xiaoke.api.service.CommentService;
import com.xiaoke.entity.Comment;
import com.xiaoke.entity.User;
import com.xiaoke.shiro.SecurityService;
import com.xiaoke.util.common.AjaxMsg;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/comment")
public class CommentController {
    @Autowired
    private CommentService commentService;
    @Autowired
    private SecurityService securityService;

    @PostMapping("/save")
    public AjaxMsg save(Comment comment,String images){
        User user = securityService.getCurrentUser();
        comment.setUserId(user.getId());
        commentService.insert(comment,images);
        return new AjaxMsg();
    }

    @GetMapping("/count")
    public AjaxMsg countByGoods(String goodsId){
        Integer allCount = commentService.countByGoods(goodsId);
        Integer hasPicCount = commentService.countByGoodsPic(goodsId);
        Map res = new HashMap();
        res.put("allCount",allCount);
        res.put("hasPicCount",hasPicCount);
        return new AjaxMsg(res);
    }

    @GetMapping("/list")
    public AjaxMsg list(String goodsId, @RequestParam(defaultValue = "1") Integer pageNum,@RequestParam(defaultValue = "10") Integer pageSize,Integer showType){
        List<Map<String, Object>> comments = commentService.findByGoodsAndPic(goodsId, pageNum, pageSize, showType);
        return new AjaxMsg(comments);
    }
}
